* GENERAL INFO
	* Project: Red/Blue Workshop and Depolarization
 
* DO FILE INFO
	* Combines baseline, midline, and endline data from all campuses
	* Variable construction

*********************************************************************************************************************



/* SETUP */

			clear
			set more off
			capture cd "~/Dropbox/Projects/Better Angels RCT/10_Analysis/03_Code/Replication/Final"		
			
			use "endline_clean_all.dta"
			renpfix e_
			drop treatment
			gen wave=3
			tempfile endline
			save `endline'
			
			use "midline_clean_all.dta"
			renpfix m_
			gen m_senior = senior
			drop treatment
			gen wave=2
			tempfile midline
			save `midline'

			use "baseline_clean_all.dta"
			renpfix b_
			gen b_white = white
			gen b_woman = woman

			gen wave=1
			
			append using `midline'
			append using `endline'
			lab var wave "Survey Wave"
			lab def wave 1 "Baseline" 2 "Midline" 3 "Endline"
			lab val wave wave
			
			
			tsset sid wave
			
			placevar sid wave
			sort sid wave
			gen uid=sid
			egen uidtag=tag(uid)

		

			drop if party=="" 
			
			
			* use treatment from baseline (not midline, embedded data)
			sort uid treatment
			bysort uid : replace treatment = treatment[_n-1] if missing(treatment) & _n > 1 & wave==2
			bysort uid : replace treatment = treatment[_n-1] if missing(treatment) & _n > 1 & wave==3
			
			tab campus, gen(campus_FE)
			rename campus campus2
			encode campus2, gen(campus)
			drop campus2
			
			gen randomized=1
				replace randomized=0 if campus==1 & party=="Red"
				replace randomized=0 if campus==4 & party=="Red"
			recode treatment (0=1) if campus==4 & party=="Red" // all Pitt reds were invited (changed to T=1)
			*recode treatment (.=0) if campus==3 & recipientname=="Allison Wilkens"
			
			gen party_num=(party=="Blue")
				la def party_num 0 "Red" 1 "Blue"
				la val party_num party_num
			
			
			* identify those in each wave
			gen baseline=(wave==1)
			egen part_base=max(baseline), by(uid)
			gen midline=(wave==2)
			egen part_mid=max(midline), by(uid)
			recode part_mid (0=1)(1=0), gen(attrit_mid)
			gen endline=(wave==3) 
			egen part_end=max(endline), by(uid)
			recode part_end (0=1)(1=0), gen(attrit_end)
		
			* merge in workshop attendance information
			preserve 
			import excel "attendance.xlsx", sheet("Sheet1") firstrow clear
			keep sid workshop
			rename workshop part_work 
			tempfile attend
			save `attend'
			restore 
			merge m:1 sid using `attend' , gen(m)
			lab var part_work "Workshop Participant"
			
			* ingroup and outgroup homogeneity
			alpha policy_imm_inparty policy_health_inparty policy_abort_inparty policy_deficit_inparty policy_guns_inparty, ///
			gen(idx_hom_in)
			replace idx_hom_in=idx_hom_in-5
			alpha policy_imm_outparty policy_health_outparty policy_abort_outparty policy_deficit_outparty policy_guns_outparty, ///
			gen(idx_hom_out)
			replace idx_hom_out=idx_hom_out-5
			
			* demographics
			encode q212, gen(gender)
			lab def gender 1 "Female" 2 "Male" 3 "Other" 4 "Prefer not to say" , replace
			lab val gender gender
			
				
			
			la var idx_affect_pol "affective polarization index"
			la var stereo_diff "outgroup stereotyping index"
			la var ind_ideo_con "individual ideological constraint index"
			la var idx_mass_ideo_pol "perceived mass ideological polarization index"
			la var idx_outgroup_empathy "outgroup empathy index"
			la var human_diff "outgroup dehumanization index"
			la var idx_moralization "moralization index"
			la var idx_ingroup_salience "ingroup identity salience index"
			la var idx_civil_support "support for civil discourse index"
			la var idx_civil_capacity "capacity for civil discourse index"
			la var idx_donate_depol "Behavioral"
			
			
			
			la var therm_diff "therm"
			la var outparty_against "against"
			la var trust_diff "trust"
			la var outparty_friends "friends"
			la var outparty_neigh "neighbors"
			la var outparty_marry "marry"
			la var outparty_threa "threat"
					

* standardize DVs to baseline mean and SD

quietly summarize idx_affect_pol if wave==1
local m = r(mean)
local s = r(sd)
gen affpol = (idx_affect_pol - `m') / `s'

quietly summarize idx_affect_pol_noneg if wave==1
local m = r(mean)
local s = r(sd)
gen affpol_noneg = (idx_affect_pol_noneg - `m') / `s'

quietly summarize idx_affect_pol_noneg_comf if wave==1
local m = r(mean)
local s = r(sd)
gen affpol_noneg_comf = (idx_affect_pol_noneg_comf - `m') / `s'

quietly summarize idx_affect_pol_noneg_out if wave==1
local m = r(mean)
local s = r(sd)
gen affpol_noneg_out = (idx_affect_pol_noneg_out - `m') / `s'

quietly summarize outparty_therm if wave==1
local m = r(mean)
local s = r(sd)
gen affpol_out_therm = (outparty_therm - `m') / `s'

quietly summarize outparty_therm_rev if wave==1
local m = r(mean)
local s = r(sd)
gen affpol_out_therm_rev = (outparty_therm_rev - `m') / `s'



quietly summarize dinparty if wave==1
local m = r(mean)
local s = r(sd)
gen iat = (dinparty - `m') / `s'

quietly summarize idx_donate_depol if wave==2 & treatment==0
local m = r(mean)
local s = r(sd)
gen donate = (idx_donate_depol - `m') / `s'

quietly summarize outparty_no_converse if wave==1
local m = r(mean)
local s = r(sd)
gen no_converse = (outparty_no_converse - `m') / `s'

quietly summarize outparty_converse if wave==1
local m = r(mean)
local s = r(sd)
gen converse = (outparty_converse - `m') / `s'

quietly summarize idx_civil_support if wave==1
local m = r(mean)
local s = r(sd)
gen civil_support = (idx_civil_support - `m') / `s'

quietly summarize idx_civil_capacity if wave==1
local m = r(mean)
local s = r(sd)
gen civil_capacity = (idx_civil_capacity - `m') / `s'





quietly summarize idx_pol_omnibus if wave==2 & treatment==0
local m = r(mean)
local s = r(sd)
gen omnibus = (idx_pol_omnibus - `m') / `s'

quietly summarize idx_pol_omnibus_conv if wave==2 & treatment==0
local m = r(mean)
local s = r(sd)
gen omnibus_conv = (idx_pol_omnibus_conv - `m') / `s'




quietly summarize stereo_diff if wave==1
local m = r(mean)
local s = r(sd)
gen m1a = (stereo_diff - `m') / `s'

quietly summarize adhom_diff if wave==3 & treatment==0 /* RB 10/24/2023: What's going on with this variable? */
local m = r(mean)
local s = r(sd)
gen m1b = (adhom_diff - `m') / `s'

quietly summarize ind_ideo_con if wave==1
local m = r(mean)
local s = r(sd)
gen m2a = (ind_ideo_con - `m') / `s'

quietly summarize ind_ideo_ext if wave==1
local m = r(mean)
local s = r(sd)
gen m2b = (ind_ideo_ext - `m') / `s'

quietly summarize idx_mass_ideo_pol if wave==1
local m = r(mean)
local s = r(sd)
gen m3 = (idx_mass_ideo_pol - `m') / `s'

quietly summarize idx_outgroup_empathy if wave==1
local m = r(mean)
local s = r(sd)
gen m4 = (idx_outgroup_empathy - `m') / `s'

quietly summarize human_diff if wave==1
local m = r(mean)
local s = r(sd)
gen m5 = (human_diff - `m') / `s'

quietly summarize idx_moralization if wave==1
local m = r(mean)
local s = r(sd)
gen m6 = (idx_moralization - `m') / `s'

quietly summarize idx_ingroup_salience if wave==1
local m = r(mean)
local s = r(sd)
gen m7 = (idx_ingroup_salience - `m') / `s'

foreach c in therm_diff outparty_against trust_diff outparty_friends outparty_neigh outparty_marry outparty_threat {
quietly summarize `c' if wave==1
local m = r(mean)
local s = r(sd)
gen `c'_std = (`c' - `m') / `s'
}

* copy baseline values by respondent
global baselinevars affpol iat affpol_noneg affpol_noneg_comf /// main DVs
			therm_diff_std outparty_against_std trust_diff_std outparty_friends_std outparty_neigh_std outparty_marry_std outparty_threat_std /// affpol components
			m1a m2a m2b m3 m4 m5 m6 m7 /// mechanisms
			ideoextreme idx_pol_engage idx_outparty_contact idx_empathy // htes
			
			* construct baseline values
			
			foreach var in $baselinevars {
			gen b_`var'=`var' if wave==1
			}
			
			* copy baseline values to midline and endline for HTE analyses & attrition analyses 
			foreach var in $baselinevars woman white {
			sort uid b_`var'
			bysort uid : replace b_`var' = b_`var'[_n-1] if missing(b_`var') & _n > 1
			}
			
			sort uid m_senior
			bysort uid : replace m_senior = m_senior[_n-1] if missing(m_senior) & _n > 1
			
			* create HTE variable categories (already categorical: party_num ideoextreme)
			recode partisan (2=1)(1=2)(3 4=3), gen(party3)
			lab def party3 1 "Democrat" 2 "Republican" 3 "Independent/Other"
			lab val  party3 party3 
			foreach h in idx_pol_engage idx_outparty_contact idx_empathy {
			xtile b_`h'_c=b_`h', n(4)
			}

			
			
			
* label variables
lab var affpol_noneg_comf "Explicit"
lab var iat "Implicit"
lab var donate "Behavioral"     
   
lab var m1a "Outgroup Stereotyping"         
lab var m1b "Outgroup Ad Hominem"         
lab var m2a "Ideological Constraint"         
lab var m2b "Ideological Extremity"        
lab var m3 "Perceived Mass Ideo. Polarization"         
lab var m4 "Outgroup Empathy"         
lab var m5 "Outgroup Humanization"         
lab var m6 "Moralization of Politics"         
lab var m7 "Ingroup Identity Salience"

lab var b_m1a "Outgroup Stereotyping (Baseline)"                
lab var b_m2a "Ideological Constraint (Baseline)"         
lab var b_m2b "Ideological Extremity (Baseline)"        
lab var b_m3 "Perceived Mass Ideo. Polarization (Baseline)"         
lab var b_m4 "Outgroup Empathy (Baseline)"         
lab var b_m5 "Outgroup Humanization (Baseline)"         
lab var b_m6 "Moralization of Politics (Baseline)"         
lab var b_m7 "Ingroup Identity Salience (Baseline)"

lab var therm_diff_std  "Partisan Feeling Thermometer Gap"       
lab var  outparty_against_std "Negative Partisanship"        
lab var  trust_diff_std "Partisan Trust Gap"        
lab var  outparty_friends_std "Comfort w/ Out Party Friends"         
lab var  outparty_neigh_std     "Comfort w/ Out Party Neighbors"    
lab var  outparty_marry_std "Comfort w/ Out Party Marriage"        
lab var  outparty_threat_std "Out Party is a Threat"

lab var b_therm_diff_std  "Partisan Feeling Thermometer Gap (Baseline)"       
lab var  b_outparty_against_std "Negative Partisanship (Baseline)"        
lab var  b_trust_diff_std "Partisan Trust Gap (Baseline)"        
lab var  b_outparty_friends_std "Comfort with Out Party Friends (Baseline)"         
lab var  b_outparty_neigh_std     "Comfort with Out Party Neighbors (Baseline)"    
lab var  b_outparty_marry_std "Comfort with Out Party Marriage (Baseline)"        
lab var  b_outparty_threat_std "Out Party is a Threat (Baseline)"

lab var b_affpol_noneg_comf "Explicit Affective Polarization (Baseline)"
lab var b_iat "Implicit Affective Polarization (Baseline)"
lab var b_ideoextreme "Extremity of Ideological Self-Placement (Baseline)"
lab var b_idx_pol_engage "Political Engagement (Baseline)" 
lab var b_idx_outparty_contact  "Outparty contact (Baseline)"
lab var b_idx_empathy "Dispositional empathy (Baseline)"


lab var party3 "Partisanship"
lab var b_idx_pol_engage_c "Baseline Political Engagement"
lab var b_idx_outparty_contact_c "Baseline Contact w/ Out Party"
lab var b_idx_empathy_c "Baseline Dispositional Empathy"

lab var attrit_mid "Midline Attritor"
lab var attrit_end "Endline Attritor"

lab var canexp_pref_inparty "Support for In Party"
lab var canexp_treat_midline "In Party Violation"
lab var canexp_treat_endline "In Party Violation"
lab var civil_support "Support for Civil Discourse"
lab var civil_capacity "Capacity for Civil Discourse"
lab var adhom_diff "Relative Out Party Ad Hominem Attribution"

lab var covid_protest_r  "Opponents of Covid Lockdowns"
lab var blm_oppose_r  "Opponents of BLM"
lab var greendeal_gen_r "Opponents of Green New Deal"
lab var elect_delay_r  "Proponents of Delaying 2020 Election"  
lab var daca_heart_r   "Opponents of DACA"
lab var healthcare_liberty_d  "Proponents of Universal Health Care"  
lab var mailin_threat_d  "Proponents of Mail-In Ballots"  
lab var police_anarch_d   "Proponents of Defunding Police" 
lab var covid_lockdown_d  "Proponents of Covid Lockdowns"  
lab var gunban_cons_d "Opponents of Assault Rifles"

lab var b_woman "Woman"
lab var b_white "White"


sort sid wave

drop if treatment==.
drop if block==.

**** Export Data *****
save "Full_BArct_Data_panel.dta", replace





		
